package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.DTO.RoleAddDto;
import com.njworkorder.DTO.AllocationMenuDto;
import com.njworkorder.Entity.AdministrativePersonnel;
import com.njworkorder.Entity.Role;
import com.njworkorder.Entity.RoleMenu;
import com.njworkorder.Service.RoleMenuService;
import com.njworkorder.Service.RoleService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.RoleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Tag(name = "角色管理", description = "角色管理相关接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    private final RoleMenuService roleMenuService;
    private final RoleService roleService;
    public RoleController(RoleService roleService, RoleMenuService roleMenuService) {
        this.roleService = roleService;
        this.roleMenuService = roleMenuService;
    }

    @Operation(summary = "添加角色")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@RequestBody RoleAddDto roleAddDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleAddDto, role);

        boolean save = roleService.save(role);
        if (save) {
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }

    @Operation(summary = "根据id删除角色")
    @GetMapping("/deleteById")
    public Result<String> deleteById(@RequestParam("id") String id) {

        boolean removeById = roleService.removeById(id);

        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<RoleMenu>()
                .eq(RoleMenu::getRoleId, id);
        roleMenuService.remove(queryWrapper);

        if (removeById) {
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }

    @Operation(summary = "根据id修改角色")
    @PostMapping("/updateById/{id}")
    public Result<String> updateById(@RequestBody RoleAddDto roleAddDto,@PathVariable("id") String id) {
        Role role = new Role();
        BeanUtils.copyProperties(roleAddDto, role);
        role.setId(id);
        boolean updateById = roleService.updateById(role);
        if (updateById) {
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.fail("修改失败");
    }

    @Operation(summary = "根据id查询角色")
    @GetMapping("/selectById")
    public Result<RoleVo> selectById(@RequestParam("id") String id) {

        MPJLambdaWrapper<Role> wrapper = new MPJLambdaWrapper<Role>()
                .selectAll(Role.class)
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId, Role::getCreateApId,ext ->
                        ext.selectAs(AdministrativePersonnel::getName, RoleVo::getCreateApName))
                .eq(Role::getId, id);

        RoleVo roleVo = roleService.selectJoinOne(RoleVo.class, wrapper);

        return ResultUtil.success(roleVo);
    }

    @Operation(summary = "查询角色列表")
    @GetMapping("/getList")
    public Result<Page<RoleVo>> getList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize) {
        MPJLambdaWrapper<Role> wrapper = new MPJLambdaWrapper<Role>()
                .selectAll(Role.class)
                .leftJoin(AdministrativePersonnel.class, AdministrativePersonnel::getId, Role::getCreateApId, ext ->
                        ext.selectAs(AdministrativePersonnel::getName, RoleVo::getCreateApName))
                .apply("1=1");

        Page<RoleVo> roleVoPage = roleService.selectJoinListPage(new Page<>(pageIndex, pageSize), RoleVo.class, wrapper);
        return ResultUtil.success(roleVoPage);
    }

    @Transactional
    @Operation(summary = "给角色分配菜单")
    @PostMapping("/allocationMenuById")
    public Result<String> allocationMenu(@RequestBody AllocationMenuDto allocationMenuDto) {

        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleMenu::getRoleId, allocationMenuDto.getId());
        roleMenuService.remove(wrapper);

        List<RoleMenu> roleMenus = new ArrayList<>();
        RoleMenu roleMenu;
        for (String menuId : allocationMenuDto.getMenuIds()) {
            roleMenu = new RoleMenu();
            roleMenu.setRoleId(allocationMenuDto.getId());
            roleMenu.setMenuId(menuId);
            roleMenu.setCreateApId(allocationMenuDto.getApId());
            roleMenus.add(roleMenu);
        }
        boolean b = roleMenuService.saveBatch(roleMenus);
        if (b) {
            return ResultUtil.success("分配成功");
        }
        return ResultUtil.fail("分配失败");
    }
}
